package com.yanqu.road.server.manger;

import com.yanqu.road.entity.beautyescort.EscortBeautyEventBossInfo;
import com.yanqu.road.entity.beautyescort.EscortClientInfo;
import com.yanqu.road.logic.bussiness.beautyescort.BeautyEscortBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.RandomHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

public class BeautyEscortConfigMgr extends TempMgr {

    /**
     * 委托人
     */
    private static Map<Integer, EscortClientInfo> clientInfoMap = new ConcurrentHashMap<>();
    /**
     * 走镖事件
     */
    private static Map<Integer, EscortBeautyEventBossInfo> eventBossInfoMap = new ConcurrentHashMap<>();


    @Override
    public boolean reloadConfig() throws Exception {
        eventBossInfoMap = BeautyEscortBussiness.getEventBossInfoMap();
        clientInfoMap = BeautyEscortBussiness.getClientInfoMap();
        getLogger().info("BeautyEscortConfigMgr Reload");
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static Map<Integer, EscortClientInfo> getClientInfoMap() {
        return clientInfoMap;
    }

    public static EscortClientInfo getEscortClientInfo(int id){
        return clientInfoMap.get(id);
    }

    public static Map<Integer, EscortBeautyEventBossInfo> getEventBossInfoMap() {
        return eventBossInfoMap;
    }

    /**
     * 随机委托人
     */
    public static int randomClient(List<Integer> exclude){
        List<Integer> idList = new ArrayList<>();
        for(int key : clientInfoMap.keySet()){
            if(!exclude.contains(key)) {
                idList.add(key);
            }
        }
        Random random = new Random();
        List<Integer> weight = new ArrayList<>();
        for(int key : idList){
            weight.add(clientInfoMap.get(key).getWeight());
        }
        int idx = RandomHelper.getRandomIndexByWeight(weight, random);
        return idList.get(idx);
    }

    /**
     * 随机商会事件
     */
    public static EscortBeautyEventBossInfo getRandomEventInfo(int unionLevel){
        List<Integer> idList = new ArrayList<>();
        List<Integer> weightList = new ArrayList<>();
        for (Map.Entry<Integer, EscortBeautyEventBossInfo> bossInfoEntry : eventBossInfoMap.entrySet()) {
            if(bossInfoEntry.getValue().getUnionLevel() > unionLevel){
                continue;
            }
            idList.add(bossInfoEntry.getKey());
            weightList.add(bossInfoEntry.getValue().getWeight());
        }
        if(idList.size() == 0){
            return null;
        }
        if(idList.size() == 1){
            return eventBossInfoMap.get(idList.get(0));
        }
        Random random = new Random();
        int idx = RandomHelper.getRandomIndexByWeight(weightList, random);
        return eventBossInfoMap.get(idList.get(idx));
    }
}
